//
//  ShopDetailCell.m
//  JewelryStore
//
//  Created by apple on 2016/12/31.
//  Copyright © 2016年 com. All rights reserved.
//

#import "ShopDetailCell.h"
#import "HCSStarRatingView.h"
#import "shopDetailImageCell.h"

#define SHOPDETAILIMAGECELL @"shopDetailImageItem"
@interface ShopDetailCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UILabel *customerName;
@property (weak, nonatomic) IBOutlet UILabel *comment;
@property (weak, nonatomic) IBOutlet UICollectionView *imageCollectView;
@property (weak, nonatomic) IBOutlet HCSStarRatingView *shopStarView;

@property (nonatomic, copy) NSArray *imageURLs;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight;
@end

@implementation ShopDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    //fit size
    [self fitLabelSize];
    
    //
    [self configureImageCollect];
    
}

- (void)setModel:(NSArray *)model
{
    _model = model;
    
    //星星
    _shopStarView.value = arc4random()%5;

}

- (void)fitLabelSize
{
    _customerName.font = Font(12);
    
    _comment.font = Font(14);
    
}

- (void)configureImageCollect
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/4.08 - 3, 80);
    
    _imageCollectView.collectionViewLayout = layout;
    
    NSInteger index = self.imageURLs.count%4 == 0 ? 0 : 1;
    
    NSInteger count = self.imageURLs.count/4  + index;
    
    _imageViewHeight.constant = count * 80;  //count
    
    _imageCollectView.delegate = self;
    
    _imageCollectView.dataSource = self;
    
    _imageCollectView.bounces = NO;
    
    _imageCollectView.scrollEnabled = NO;

    [_imageCollectView registerNib:[UINib nibWithNibName:@"shopDetailImageCell" bundle:nil] forCellWithReuseIdentifier:SHOPDETAILIMAGECELL];
    
}

#pragma mark - collect delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageURLs.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    shopDetailImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SHOPDETAILIMAGECELL forIndexPath:indexPath];
    
    return cell;
}

#pragma mark - collect layout

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.1;
}

#pragma mark - lazy

- (NSArray *)imageURLs
{
    if (!_imageURLs) {
        
        _imageURLs = @[@"", @"", @"", @"", @"", @""];
    }
    return _imageURLs;
}
@end
